package com.dtkavin.hadoop;

import com.dtkavin.hadoop.iface.HdfsI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

/**
 * hdfs 简单文件操作
 * Created by john on 10/1/16.
 */
public class HdfsSimpleOperation implements HdfsI{
    private Configuration conf;

    //初始化，获得配置
    public HdfsSimpleOperation(Configuration conf) {
        this.conf = conf;
    }

    /**
     * 文件上传
     *
     * @param srcPath 文件输入路径
     * @param dstPath 文件输出路径
     */
    public void uploadFile(String srcPath, String dstPath) {
        FileSystem fs = null;
        try {
            fs = FileSystem.get(new URI("hdfs://node01:9000"), conf, "hadoop");
            fs.copyFromLocalFile(false, true, new Path(srcPath), new Path(dstPath));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fs != null) {
                try {
                    fs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 文件下载
     * @param srcPath 文件输入路径
     * @param dstPath 文件输出路径
     */
    public void downloadFile(String srcPath, String dstPath) {
        FileSystem fs = null;
        try {
            fs = FileSystem.get(new URI("hdfs://node01:9000"), conf, "hadoop");
            fs.copyToLocalFile(false, new Path(srcPath), new Path(dstPath), true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fs != null) {
                try {
                    fs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
